<template>
	<view class="pages">
		<view class="tips">
			{{subject.subjectName}} > {{subject.subjectName2}} > {{subject.collectionName}} > {{subject.chapterName}}
		</view>
		<view style="height: 90rpx;"></view>
		<view>
			<view class="u-font-28 color333 flex align-center">
				<view class="edit u-m-r-10">
					<u-icon name="edit-pen" size="36" color="#ffffff"></u-icon>
				</view>
				输入节的名称
			</view>
			<view class="u-font-24 color999 u-m-l-50">建议选择公共名称，可产生链接，让更多的用户看到</view>
		</view>
		<view class="bg-white inputChange u-m-t-20 flex">
			<u-input class="u-flex-1 u-font-28" :clearable="false" placeholder=" " v-model="keyWord"></u-input>
		</view>
		<!-- 公共名字 -->
		<view class="u-m-t-30">
			<view class="flex align-center">
				<u-icon
					name="https://aihelp.oss-cn-hangzhou.aliyuncs.com/public/base/2023/11/22/c9bebc8f61a14659941439afefe7724d.png"
					size="40"></u-icon>
				<view class="color333 bold u-font-28 u-m-l-5">公共节名字</view>
				<view class="u-font-24 color999 u-m-l-5">（点文字选中，点小图标看关键词）</view>
			</view>
			<view class="flex align-center flex-wrap">
				<view class="u-m-r-35 bg-white u-p-10 flex align-center justify-center u-font-28 u-m-t-20"
					v-for="(item,index) in list" :key="index" @click="handleSelect(item)">
					{{item.label}}
				</view>
			</view>
			<view class="u-p-b-30 u-p-t-30" v-if="list && list.length">
				<u-loadmore @loadmore="handleListBottom" font-size="24" color="#999" :status="status" />
			</view>
		</view>

		<!-- 底部按钮 -->
		<view class="flex align-center justify-center know-btns" :style="'padding-bottom:'+safeAreaInsetsBottom+'px;'" v-if="false">
			<view class="close vertical-align">
				以后
				<text class="u-font-24 colorMain">在个人中心</text>
				创建
			</view>
			<view class="submit" @click="submit">确定创建</view>
		</view>
		<view class="flex align-center justify-center know-btns" :style="'padding-bottom:'+safeAreaInsetsBottom+'px;'">
			<view class="submit1" @click="submit">确定创建</view>
		</view>
		<u-select @cancel="cancel" confirm-text="确认更换" cancel-color="#333" :mask-close-able="false" confirm-color="#333"
			cancel-text="不更换" safe-area-inset-bottom title="已有相似名称,请选择" v-model="show" mode="single-column" :list="selectList"
			@confirm="handleSelect">
			<u-icon class="u-m-l-10" slot="titleSolt"
				name="https://aihelp.oss-cn-hangzhou.aliyuncs.com/public/base/2023/11/23/caa2ade34bc64623b674585a04caa628.png"
				size="64"></u-icon>
		</u-select>
	</view>
</template>

<script>
	import {
		treeSave,
		treeUpdate,
		TreePublicQuery,
		findTreePrivate
	} from "@/api/send.js";
	export default {
		data() {
			return {
				status:"loadmore",
				page:1,
				size:12,
				isEdit: false,
				subject: {},
				safeAreaInsetsBottom: 0,
				selectList: [],
				show: false,
				keyWord: "",
				list: [],
				publicObj: {}
			}
		},
		onLoad(e) {
			if (uni.getStorageSync('subject')) {
				this.subject = JSON.parse(uni.getStorageSync('subject'));
			}
			this.getPublicTree();
			if (e && e.key == 'edit') {
				this.isEdit = true;
				this.keyWord = JSON.parse(JSON.stringify(this.subject.jonitName));
				uni.setNavigationBarTitle({
					title: "修改节"
				})
			}
			this.$nextTick(() => {
				this.safeAreaInsetsBottom = this.$u.sys().safeAreaInsets.bottom;
			})
		},
		methods: {
			handleListBottom() {
				if (this.status === 'nomore') return;
				this.page++;
				this.status = 'loading';
				this.getPublicTree();
			},
			//选择公有名称
			handleSelect(e) {
				if (!e.label) {
					e = e[0];
				}
				this.publicObj = e;
				this.keyWord = e.label;
				this.confirm(true);
			},
			//获取公有节
			getPublicTree() {
				if (!this.subject.publicId) return;
				TreePublicQuery({
					page: this.page,
					size: this.size,
					partId: this.subject.publicId,
					treeType: 2
				}).then(res => {
					if (res.code == 100000) {
						if (this.page == 1) {
							this.list = [];
						}
						if (res.data && res.data.list && res.data.list.length) {
							let arr = res.data.list.map(e => {
								return {
									label: e.treeName,
									id: e.id,
								}
							})
							this.list.push(...arr);
						}
						if (Number(res.data.tatol) <= this.list.length) {
							this.status = 'nomore';
						} else {
							this.status = 'loadmore';
						}
					}
					console.log('res: ', this.list);
				})
			},
			//不更换
			cancel() {
				this.confirm();
			},
			//更换
			confirm(isCheck = false) {
				if (uni.getStorageSync('subject')) {
					let data = {
						parentId: this.subject.chapterId,
						treePrivateState: 1,
						treePrivateName: this.keyWord,
						type: 2, //节
						subjectCode: this.subject.subjectCode,
						subjectCode2: this.subject.subjectCode2,
						subjectName: this.subject.subjectName,
						subjectName2: this.subject.subjectName2
					}

					let str = "节创建成功!";
					data['treePublicId'] = isCheck ? this.publicObj.id : '';
					data['treePublicName'] = isCheck ? this.publicObj.name : '';
					uni.showLoading({
						mask: true
					});
					if (this.isEdit) {
						data['id'] = this.subject.jonitId;
						str = "节修改成功!";
						if (this.subject.jonitName == this.keyWord) {
							treeUpdate(data).then(res => {
								if (res.code == 100000) {
									uni.showToast({
										title: str,
										icon: 'none'
									});
									uni.removeStorageSync('subject');
									setTimeout(() => {
										uni.navigateBack(1);
									}, 1000)
								}
							}).finally(() => {
								uni.hideLoading();
							})
						} else {
							findTreePrivate(this.subject.jonitId).then(obj => {
								if (obj.code == 100000) {
									let info = obj.data;
									for (let key in data) {
										info[key] = data[key];
									}
									treeSave(info).then(res => {
										if (res.code == 100000) {
											uni.showToast({
												title: str,
												icon: 'none'
											});
											uni.removeStorageSync('subject');
											setTimeout(() => {
												uni.navigateBack(1);
											}, 1000)
										}
									}).finally(() => {
										uni.hideLoading();
									})
								}
							})
						}
					} else {
						treeSave(data).then(res => {
							if (res.code == 100000) {
								uni.removeStorageSync('subject');
								uni.showToast({
									title: str,
									icon: 'none'
								});
								setTimeout(() => {
									uni.navigateBack(1);
								}, 1000)
							}
						}).finally(() => {
							uni.hideLoading();
						})
					}
				}
			},
			submit() {
				if (!this.keyWord) {
					uni.showToast({
						title: '节的名称不能为空！',
						icon: 'none'
					});
					return;
				}
				uni.showLoading({
					mask: true
				});
				TreePublicQuery({
					partId: this.subject.publicId,
					page: 1,
					size: 30,
					treeName: this.keyWord,
					treeType: 2
				}).then(res => {
					console.log('res: ', res);
					if (res.code == 100000) {
						if (res.data.list && res.data.list.length) {
							this.selectList = res.data.list.map(e => {
								return {
									value: e.id,
									label: e.treeName
								}
							})
							this.show = true;
						} else {
							this.confirm();
						}
					}
				}).finally(() => {
					uni.hideLoading();
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.pages {
		background-color: #F6F8F6;
		min-height: 100vh;
		padding: 40rpx 30rpx;

		.tips {
			color: #73839A;
			font-size: 24rpx;
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			background-color: rgba(255, 255, 221, .8);
			height: 90rpx;
			display: flex;
			align-items: center;
			padding-left: 40rpx;
			word-break: break-all;
			padding-right: 40rpx;
		}

		.edit {
			background-color: #000;
			width: 40rpx;
			height: 40rpx;
			border-radius: 8rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.inputChange {
			height: 84rpx;
		}

		.key-box {
			min-height: 248rpx;
			border-bottom: 2rpx solid #000;

			.tag-item {
				border-radius: 100rpx;
				height: 64rpx;
				padding-left: 20rpx;
				padding-right: 20rpx;
				font-size: 28rpx;
				color: #7F7F7F;
				margin-right: 36rpx;
				margin-bottom: 10rpx;
			}

			.add-key {
				background-color: #333;
				font-size: 24rpx;
				color: #F7DD5E;
				width: 134rpx;
				height: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 100rpx;
			}
		}

		.know-btns {
			position: fixed;
			left: 50%;
			transform: translate(-50%);
			font-size: 28rpx;
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			bottom: 60rpx;

			.submit1 {
				border-radius: 100rpx;
				color: #333;
				background-color: #F7DD5E;
				width: 600rpx;
			}

			.close {
				border-radius: 100rpx 0 0 100rpx;
				background-color: #72819A;
				width: 300rpx;
			}

			.submit {
				border-radius: 0 100rpx 100rpx 0;
				color: #333;
				background-color: #F7DD5E;
				width: 300rpx;
			}
		}
	}

	.add-key-word {
		background-color: #F7DD5E;
		border-radius: 100rpx;
		width: 300rpx;
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 30rpx auto 0;
	}
</style>